/*********************************************************
*
*  FJH-sim v1.0
*  Copyright � 2008 H.Y. Bruintjes, J.H. Donker,
*  C.E. Etheredge
*
*  Protocol stack base class
*
**********************************************************/

#include "protocol/ProtocolStack.h"
#include "protocol/NetworkLayer.h"
#include "protocol/AltBitTransportLayer.h"

#include <iostream>

ProtocolStack::ProtocolStack(Node *node) : m_nextLayer(0)
{
	m_node = node;

	// construct our default protocol stack bottom-up:
	//   ProtocolStack
	//     TransportLayer
	//       NetworkLayer
	//         Node

	NetworkLayer			*ln	 = new NetworkLayer(node);
	TransportLayer			*lt	 = new TransportLayer(ln);
	AltBitTransportLayer	*alt = new AltBitTransportLayer(ln);
	m_nextLayer = lt;
	m_routing = ln->getRouting();
}

ProtocolStack::~ProtocolStack()
{
	// delete the next layer in the stack
	SAFE_DELETE (m_nextLayer);
}

bool ProtocolStack::open()
{
	return m_nextLayer->open();
}

void ProtocolStack::close()
{
	m_nextLayer->close();
	return;
}

int ProtocolStack::send(const char* data, unsigned int length, NodeAddress destination)
{
	// perform packet splitting
	return m_nextLayer->send(data,length,destination);
}

int ProtocolStack::receive(char* data, unsigned int length, NodeAddress &source)
{
	// perform packet merging
	return m_nextLayer->receive(data,length,source);
}

int ProtocolStack::select(ServiceRequest service, void * pData)
{
	return m_nextLayer->select(service, pData);	//pass on to TransportLayer
}

void ProtocolStack::update()
{
	m_nextLayer->update();
	return;
}

void ProtocolStack::getFriends(std::vector<NodeAddress>& myVector)
{
	m_routing->getFriends(myVector);
}

void ProtocolStack::getAcquaintances(std::vector<NodeAddress>& myVector)
{
	m_routing->getAcquaintances(myVector);
}


